<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>教师管理系统</title>
    <link rel="stylesheet" href="https://www.layuicdn.com/layui-v2.6.8/css/layui.css">
    <style>
        body {
            margin: 0;
            font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", Arial, sans-serif;
            background-color: #f5f7fa;
        }
        .app-container {
            display: flex;
            min-height: 100vh;
        }
        .sidebar {
            width: 200px;
            background: linear-gradient(135deg, #2c3e50, #4a69bd);
            color: white;
            padding: 15px 0;
            box-shadow: 2px 0 6px rgba(0, 0, 0, 0.1);
        }
        .sidebar-header {
            padding: 0 15px 15px;
            border-bottom: 1px solid rgba(255, 255, 255, 0.1);
            margin-bottom: 15px;
            text-align: center;
        }
        .sidebar-menu {
            padding: 0 15px;
        }
        .sidebar-menu a {
            display: block;
            color: #bfcbd9;
            padding: 12px 15px;
            margin: 5px 0;
            border-radius: 4px;
            text-decoration: none;
            transition: all 0.3s;
            font-size: 14px;
        }
        .sidebar-menu a:hover, .sidebar-menu a.active {
            color: #ffffff;
            background-color: rgba(255, 255, 255, 0.1);
            transform: translateX(5px);
        }
        .main-content {
            flex: 1;
            padding: 20px;
            background-color: #f5f7fa;
        }
        .header {
            background-color: white;
            padding: 15px 20px;
            margin-bottom: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 12px 0 rgba(0,0,0,0.05);
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .page-title {
            font-size: 22px;
            font-weight: 600;
            color: #303133;
            margin: 0;
        }
        .card {
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 12px 0 rgba(0,0,0,0.05);
            padding: 20px;
            margin-bottom: 20px;
        }
        .search-box {
            margin-bottom: 20px;
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 12px 0 rgba(0,0,0,0.05);
        }
        .search-form {
            display: flex;
            flex-wrap: wrap;
            gap: 15px;
        }
        .search-item {
            flex: 1;
            min-width: 200px;
        }
        .search-actions {
            flex: 0 0 30%;
            display: flex;
            align-items: flex-end;
            justify-content: flex-end;
        }
        .toolbar {
            margin-bottom: 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        .el-table {
            margin-top: 15px;
            width: 100%;
        }
        .el-table th {
            background-color: #f5f7fa !important;
        }
        .el-pagination {
            margin-top: 20px;
            text-align: right;
        }
        .el-dialog {
            border-radius: 12px;
            overflow: hidden;
            width: 500px;
        }
        .el-dialog__header {
            padding: 20px;
            border-bottom: 1px solid #e4e7ed;
            background: linear-gradient(135deg, #f5f7fa, #ffffff);
        }
        .el-dialog__body {
            padding: 20px;
        }
        .el-dialog__footer {
            padding: 15px 20px 20px;
            border-top: 1px solid #e4e7ed;
            text-align: right;
        }
        .user-info {
            display: flex;
            align-items: center;
        }
        .user-avatar {
            width: 40px;
            height: 40px;
            border-radius: 50%;
            margin-right: 10px;
            background-color: #4a69bd;
            color: white;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 20px;
        }
        .layui-btn {
            margin: 0 5px;
        }
        .layui-form-item {
            margin-bottom: 15px;
        }
        .layui-table-page {
            margin-top: 15px;
        }
        .layui-form-label {
            padding: 9px 15px;
            width: 100px !important;
        }
        .layui-input-block {
            margin-left: 100px !important;
        }
        .layui-table-header {
            background-color: #f8f9fa;
        }
        .layui-table-cell {
            padding: 8px 10px;
        }
    </style>
</head>
<body>
<div class="app-container">
    <div class="sidebar">
        <div class="sidebar-header">
            <h3 style="margin: 0; font-weight: bold;">教务管理</h3>
            <p style="font-size: 12px; color: #bfcbd9;">管理系统</p>
        </div>
        <div class="sidebar-menu">
            <a href="#" class="active"><i class="layui-icon layui-icon-user"></i> 教师管理</a>
            <a href="#"><i class="layui-icon layui-icon-username"></i> 学生管理</a>
            <a href="#"><i class="layui-icon layui-icon-read"></i> 课程管理</a>
            <a href="#"><i class="layui-icon layui-icon-search"></i> 成绩查询</a>
            <a href="#"><i class="layui-icon layui-icon-set"></i> 系统设置</a>
        </div>
    </div>

    <div class="main-content">
        <div class="header">
            <h2 class="page-title">教师管理系统</h2>
            <div class="user-info">
                <div class="user-avatar">管</div>
                <div class="user-name">管理员</div>
            </div>
        </div>

        <div class="search-box">
            <form class="layui-form search-form">
                <div class="search-item">
                    <label class="layui-form-label">教师姓名</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" placeholder="请输入教师姓名" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="search-item">
                    <label class="layui-form-label">学工号</label>
                    <div class="layui-input-block">
                        <input type="text" name="employeeNumber" placeholder="请输入学工号" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="search-actions">
                    <button type="button" class="layui-btn" lay-submit lay-filter="searchForm">
                        <i class="layui-icon layui-icon-search"></i> 搜索
                    </button>
                    <button type="button" class="layui-btn layui-btn-normal" id="addTeacher">
                        <i class="layui-icon layui-icon-add-1"></i> 添加教师
                    </button>
                </div>
            </form>
        </div>

        <table id="teacherTable" lay-filter="teacherTable"></table>
    </div>
</div>

<!-- 工具栏模板 -->
<script type="text/html" id="tableToolbar">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="edit">
            <i class="layui-icon layui-icon-edit"></i> 编辑
        </button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="del">
            <i class="layui-icon layui-icon-delete"></i> 删除
        </button>
    </div>
</script>

<!-- 添加/编辑表单模板 -->
<script type="text/html" id="teacherForm">
    <form class="layui-form" lay-filter="teacherForm" lay-submit>
        <input type="hidden" name="id">
        <div class="layui-form-item">
            <label class="layui-form-label">学工号</label>
            <div class="layui-input-block">
                <input type="text" name="employeeNumber" required lay-verify="required" placeholder="请输入学工号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-block">
                <input type="text" name="name" required lay-verify="required" placeholder="请输入姓名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">性别</label>
            <div class="layui-input-block">
                <input type="radio" name="gender" value="1" title="男" checked>
                <input type="radio" name="gender" value="2" title="女">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">年龄</label>
            <div class="layui-input-block">
                <input type="number" name="age" required lay-verify="required|number" placeholder="请输入年龄" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">入职日期</label>
            <div class="layui-input-block">
                <input type="text" name="entryDate" required lay-verify="required" placeholder="请选择入职日期" autocomplete="off" class="layui-input" id="entryDate">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">修改日期</label>
            <div class="layui-input-block">
                <input type="text" name="modifyDate" placeholder="系统自动生成" autocomplete="off" class="layui-input" readonly>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="password" name="password" placeholder="不修改请留空" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="submit" class="layui-btn" lay-submit lay-filter="saveTeacher">保存</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</script>

<script src="https://www.layuicdn.com/layui-v2.6.8/layui.js"></script>
<script>
    layui.use(['table', 'form', 'layer', 'laydate'], function(){
        var table = layui.table,
            form = layui.form,
            layer = layui.layer,
            laydate = layui.laydate,
            $ = layui.jquery;

        // 检查登录状态
        if (!localStorage.getItem('token')) {
            window.location.href = '/login.html';
            return;
        }

        // 初始化日期选择器
        laydate.render({
            elem: '#entryDate',
            trigger: 'click'
        });

        // 初始化表格
        table.render({
            elem: '#teacherTable',
            url: '/api/getTeachers',
            method: 'get',
            headers: {
                'Authorization': 'Bearer ' + localStorage.getItem('token')
            },
            where: {
                limit: 10
            },
            height: 500,
            page: true,
            limit: 10,
            limits: [10],
            cols: [[
                {field: 'employeeNumber', title: '学工号', width: '15%', height: 50},
                {field: 'name', title: '姓名', width: '15%', height: 50},
                {field: 'gender', title: '性别', width: '10%', height: 50, templet: function(d){
                    return d.gender === '1' ? '男' : '女';}},
                {field: 'age', title: '年龄', width: '10%', height: 50},
                {field: 'entryDate', title: '入职日期', width: '15%', height: 50},
                {field: 'modifyDate', title: '修改日期', width: '15%', height: 50},
                {title: '操作', toolbar: '#tableToolbar', width: '20%', fixed: 'right', height: 50}
            ]],
            parseData: function(res){
                return {
                    "code": res.code === 0 ? 0 : -1,
                    "msg": res.msg,
                    "count": res.count,
                    "data": res.data
                };
            }
        });

        // 搜索框表单提交
        form.on('submit(searchForm)', function(data){
            // 构建查询参数
            var queryParams = {};
            
            // 只有当输入值不为空时才添加到查询参数中
            if(data.field.name && data.field.name.trim() !== '') {
                queryParams.name = data.field.name.trim();
            }
            if(data.field.employeeNumber && data.field.employeeNumber.trim() !== '') {
                queryParams.employeeNumber = data.field.employeeNumber.trim();
            }
            
            // 重载表格
            table.reload('teacherTable', {
                where: queryParams,
                page: {
                    curr: 1
                }
            });
            return false;
        });

        // 表格工具栏事件
        table.on('tool(teacherTable)', function(obj){
            var data = obj.data;
            if(obj.event === 'edit'){
                showForm('编辑教师', data);
            } else if(obj.event === 'del'){
                layer.confirm('确认删除该教师信息吗？', function(index){
                    deleteTeacher(data.id);
                    layer.close(index);
                });
            }
        });

        // 添加教师按钮点击事件
        $('#addTeacher').on('click', function(){
            showForm('添加教师');
        });

        // 显示表单
        function showForm(title, data){
            layer.open({
                type: 1,
                title: title,
                area: ['500px', '600px'],
                content: $('#teacherForm').html(),
                success: function(layero, index){
                    form.render(null, 'teacherForm');
                    
                    // 添加日期选择器
                    laydate.render({
                        elem: '#entryDate',
                        trigger: 'click'
                    });
                    
                    if(data){
                        // 编辑时设置表单数据
                        form.val('teacherForm', data);
                    }

                    // 监听表单提交
                    form.on('submit(saveTeacher)', function(formData){
                        // 处理日期格式
                        if(formData.field.entryDate) {
                            formData.field.entryDate = formData.field.entryDate.replace(/\//g, '-');
                        }
                        
                        // 如果是编辑模式，确保ID被包含
                        if(data && data.id) {
                            formData.field.id = data.id;
                        }
                        
                        saveTeacher(formData.field, index);
                        return false;
                    });
                }
            });
        }

        // 保存教师信息
        function saveTeacher(data, index){
            var url = data.id ? '/api/updateTeacher' : '/api/addTeacher';
            
            // 如果密码为空且是编辑模式，删除密码字段
            if(data.id && !data.password) {
                delete data.password;
            }
            
            // 删除修改日期字段，由后端自动生成
            delete data.modifyDate;
            
            $.ajax({
                url: url,
                method: 'POST',
                headers: {
                    'Authorization': 'Bearer ' + localStorage.getItem('token')
                },
                data: JSON.stringify(data),
                contentType: 'application/json',
                success: function(res){
                    if(res.code === 0){
                        layer.msg(data.id ? '更新成功' : '添加成功');
                        layer.close(index);
                        table.reload('teacherTable');
                    } else {
                        layer.msg(res.msg || '操作失败');
                    }
                },
                error: function(xhr){
                    if(xhr.status === 401){
                        localStorage.removeItem('token');
                        window.location.href = '/login.html';
                    } else {
                        layer.msg('操作失败');
                    }
                }
            });
        }

        // 删除教师
        function deleteTeacher(id){
            $.ajax({
                url: '/api/deleteTeacher',
                method: 'POST',
                headers: {
                    'Authorization': 'Bearer ' + localStorage.getItem('token')
                },
                data: JSON.stringify({id: id}),
                contentType: 'application/json',
                success: function(res){
                    if(res.code === 0){
                        layer.msg('删除成功');
                        table.reload('teacherTable');
                    } else {
                        layer.msg(res.msg || '删除失败');
                    }
                },
                error: function(xhr){
                    if(xhr.status === 401){
                        localStorage.removeItem('token');
                        window.location.href = '/login.html';
                    } else {
                        layer.msg('删除失败');
                    }
                }
            });
        }
    });
</script>
</body>
</html>